#!/bin/bash
# -*- coding: utf-8 -*-
#Filename:   update.sh
#Author:     Yaoyi
#Email:      yaoyi@zzvcom.com
#Date:       2019-05-24 10:55:48
#Desc:       

#source function
source ./source/common/functions.sh
#set global Var
START_TIME=$SECONDS
START_DATE=`date +%y-%m-%d-%H:%M`
NAME=`basename $0`
export HOME_DIR=`pwd`
PACKAGENAME=`basename ${HOME_DIR}`
SYS=`echo ${PACKAGENAME}|awk -F "[-_]" '{print $2}'`
TASKNUM=`echo ${PACKAGENAME}|awk -F "-" '{print $NF}'`
SvnPath=`readIniSections ${CONF_FILE} SvnPath END`
UpdateProgram=`readIniSections ${CONF_FILE} Upgrade Process|cut -d "=" -f1`
LocalIp=`GetLocalIp`
if ! [ X`ls ${HOME_DIR}/../${PACKAGENAME}.tgz 2>/dev/null` = X ]
then
	MD5=`md5sum ${HOME_DIR}/../${PACKAGENAME}.tgz|awk -F " " '{print $1}'`
	SIZE=`du -sb ${HOME_DIR}/../${PACKAGENAME}.tgz|awk -F " " '{print $1}'`
else
	MD5=0000000000000
	SIZE=0000000000000
fi
BE_BAK_DIR=${HOME_DIR}/source/script/before_backup
BACKUPED_DIR=${HOME_DIR}/source/script/backuped
BE_UP_DIR=${HOME_DIR}/source/script/before_update
UPDATED_DIR=${HOME_DIR}/source/script/updated
BE_BOT_DIR=${HOME_DIR}/source/script/before_boot
BOOTED_DIR=${HOME_DIR}/source/script/booted

export CONF_FILE=${HOME_DIR}/source/conf/DIRCONFIG.ini
SERVICES=`readIniSections ${CONF_FILE} Service Process`
CHARDOG_DIR=/home/chargesoftdog
CHARDOG_FILE=${CHARDOG_DIR}/chargeSoftDog.conf
LOG_DIR=/etc/UpdatePackageRecord
TEST_LOG_DIR=/usr/local/etc/UpdatePackageRecord
CHECK_LOG_DIR="/usr/local/etc/install_check/chklog"
#function
source ./source/common/bussinessfun.sh
#####

#MAIN

#run shell before backup
	ExecShell ${BE_BAK_DIR}
	[ $? -ne 0 ] && exit 1
#end before backup

#Backup
	Backupfunction
#Backup end

#run shell backuped
	ExecShell ${BACKUPED_DIR}
	[ $? -ne 0 ] && exit 1
#end backuped

#run shell before update
	ExecShell ${BE_UP_DIR}
	[ $? -ne 0 ] && exit 1
#end before update

#Update Programe
cd ${HOME_DIR}
yaoecho hi "Update start........................."
	#kill process
		Processmanage stop
	#end kill process

	#Stop web server
		Services stop
	#End Stop web server

	#update file
		UpgradeFile
	#end update file
#Update end

#run shell updated
    ExecShell ${UPDATED_DIR}
	[ $? -ne 0 ] && exit 1
#end updated

#run shell before boot server
    ExecShell ${BE_BOT_DIR}
	[ $? -ne 0 ] && exit 1
#end before boot server

#Boot process
	Processmanage start
#Boot end

#Boot web-server
	Services start
#Boot web-server end

#run shell booted server
	ExecShell ${BOOTED_DIR}
	[ $? -ne 0 ] && exit 1
#end booted
yaoecho hg "  Execute finished."
ExecuteResult=success

#Check
cd ${HOME_DIR}/check
Result=`sh CheckServerAll.sh "${CHECK_LOG_DIR}"|grep "FAILED"`
echo -n "checkResult:"
[ X"${Result}" = X ] && yaoecho hg "PASSED" || yaoecho hr "FAILED"
yaoecho "This result is saved as ${CHECK_LOG_DIR}"
#Check end
END_TIME=$SECONDS
END_DATE=`date "+%Y-%m-%d %H:%M:%S"`

[ $# -eq 0 ] && RecodeLog
#Recode test log end

#delete updatepackage
cd ${HOME_DIR}/../
rm -rf ${PACKAGENAME:=yaoyi2847}.tgz
rm -rf ${PACKAGENAME:=yaoyi2847}
#


